var express = require('express');
const { connection } = require('../model/db') // 获取连接实例
const router = express.Router()
var multiparty = require('multiparty');
const jwt = require('jsonwebtoken');

// 示例
// router.get("/msglist", (req, res) => {
//     connection.query(`select * from user;`, (err, data) => {
//         if (err) {
//             console.log("err", err)
//             res.send('query error')
//         } else {
//             // 将 MySQL 查询结果作为路由返回值
//             res.send({
//                 code: 200,
//                 msg: "获取数据成功",
//                 data
//             })
//         }
//     })
// })

// 设置开始考试接口
router.post('/startTest', (req, res) => {
    let { id } = req.body
    let sql = `UPDATE dept_paper SET is_open=1 WHERE id=${id}`
    connection.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "设置开始考试失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "设置开始考试成功"
            })
        }
    })
})

// 老师端登录接口
router.post('/teacherLogin', (req, res) => {
    let { username, password } = req.body
    let sql = `select id,password,dept_id,teacher_name from teacher where username='${username}'`
    connection.query(sql, (err, data) => {
        if (err) {
            console.log(`err`, err);
            res.send({
                code: 500,
                msg: "登录失败"
            })
        } else {
            if (data.length < 1) {
                res.send({
                    code: 204,
                    msg: "账号错误"
                })
            } else if (data[0].password != password) {
                res.send({
                    code: 204,
                    msg: "密码错误"
                })
            } else {
                let token = "Bearer " + jwt.sign({ username, password }, 'yv', { expiresIn: 100800 })
                res.send({
                    code: 200,
                    user_id: data[0].id,
                    user_name:data[0].teacher_name,
                    dept_id:data[0].dept_id,
                    token,
                    msg: "登录成功"
                })
            }

        }
    })
})

// 获取登录用户(老师)的信息
router.get("/getTeacherUserData", (req, res) => {
    let { id } = req.query
    let sql = `select t.*,r.role_name,r.mark,d.dept_name from teacher t join role r join department d on t.role_id = r.id and t.dept_id = d.id where t.id=${id}`
    connection.query(sql, (err, data) => {
        if (err) {
            console.log("err", err)
            res.send({
                code: 500,
                msg: "获取信息失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "获取数据成功",
                data: data[0]
            })
        }
    })
})
// 添加老师信息(单条)
router.post('/addTeacher', (req, res) => {
    let { password, username, teacher_name, dept_id, role_id } = req.body
    let sql = `INSERT INTO teacher(username,password,teacher_name,dept_id,role_id)VALUES('${username}','${password}','${teacher_name}',${dept_id},${role_id})`
    connection.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "添加老师信息失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "添加老师信息成功"
            })
        }
    })
})
// 获取老师信息
router.get('/getTeacherData', (req, res) => {
    let { teacher_name, dept_id } = req.query
    let sql = ''
    if (teacher_name && dept_id) {
        sql = `select t.*,r.role_name,d.dept_name from teacher t join role r join department d on t.role_id = r.id and t.dept_id = d.id where teacher_name='${teacher_name}' and dept_id=${dept_id}`
    } else if (teacher_name) {
        sql = `select t.*,r.role_name,d.dept_name from teacher t join role r join department d on t.role_id = r.id and t.dept_id = d.id where teacher_name='${teacher_name}'`
    } else if (dept_id) {
        sql = `select t.*,r.role_name,d.dept_name from teacher t join role r join department d on t.role_id = r.id and t.dept_id = d.id where dept_id=${dept_id}`
    } else {
        sql = `select t.*,r.role_name,d.dept_name from teacher t join role r join department d on t.role_id = r.id and t.dept_id = d.id`
    }
    connection.query(sql, (err, data) => {
        if (err) {
            res.send({
                code: 500,
                msg: "获取老师信息失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "获取老师信息成功",
                data
            })
        }
    })
})
module.exports = router;